<?php

class Pager
{
	public $resultsPerPage;
	public $currentPage;
	public $CONFIG;
	
	public function __construct( $currentPage = 1,$resultsPerPage = 5)
	{
		$this->CONFIG = Zend_Registry::get('config');
		$this->currentPage = $currentPage;
		$this->resultsPerPage = $resultsPerPage;
	}
	
	public function pagination($query,$serValue='')
	{
		$pager = new Doctrine_Pager($query,
		      $this->currentPage, // Current page of request
		      $this->resultsPerPage // (Optional) Number of results per page. Default is 25
		);
		
		$result['result'] = $pager->execute(array(1), Doctrine::HYDRATE_ARRAY);
		
		$result['pages'] = $pager->getLastPage();
		
		$pagerRange = $pager->getRange(
		      'Sliding',
		      array(
		            'chunk' => 5
		      )
		);
		
		// Retrieves the range around the current page
		// In our example, we are using sliding style and we are at page 1
		$pages = $pagerRange->rangeAroundPage();

		// Outputs: [1][2][3][4][5]
		$pagerLinks = null;
		
		$Previous		=	$pager->getPreviousPage();
		$Next			= 	$pager->getNextPage();
		
		if($this->currentPage > '1') {
		if($serValue!='')
		{
			$pagerLinks[] .= '<ul><li><a href="#" onClick="ajaxPager(' . $Previous . ','. "'".$serValue."'".'); return false;"> Previous</a></li>';
		} else {
			$pagerLinks[] .= '<ul><li><a href="#" onClick="ajaxPager(' . $Previous . '); return false;"> Previous</a></li>';
		}
		} else {
			//$pagerLinks[] = '<ul><li><a> Previous</a></li>';
			$pagerLinks[] = '';
		}
			
		foreach($pages as $link)
		{
			if($link == $this->currentPage) {
				$pagerLinks[] = '<li><a href="#" class="active" style="cursor:pointer"  onClick="ajaxPager(' . $link . '); return false;"><strong>' . $link . '</strong></a></li>';
			} else {
				$pagerLinks[] = '<li><a href="#" onClick="ajaxPager(' . $link . '); return false;">' . $link . '</a></li>';
			}
		}
		
		if($this->currentPage == $pager->getLastPage()) {
			//$pagerLinks[] = '<li> <a>Next &raquo;</a></li>';
			$pagerLinks[] = '';
		} else {
		if($serValue!='')
		{
			$pagerLinks[] = '<li><a  href="#" onClick="ajaxPager(' . $Next . ','."'".$serValue."'".'); return false;"> Next</a></li></ul>';
		} else {
			$pagerLinks[] = '<li><a  href="#" onClick="ajaxPager(' . $Next . '); return false;"> Next</a></li></ul>';
		}
			
		}
		
		$result['pager'] = implode(' ', $pagerLinks);
		$result['currentPage'] = $pager->getPage();
		$result['resultPerPage'] = $this->resultsPerPage;
		$result['resultsCount'] = $pager->getNumResults();
	//	print_r($result);
		return $result;
	}
}
